﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;
using MESCodeFile;
namespace mes_tublay
{
    //Attribute definitions
    public partial class frmInventorySystem
    {
        //Modes and Flags
        enum Mode { SEARCH, ITEM_INSERT, ITEM_UPDATE, ENGINE, PR_INSERT, PR_UPDATE, AOB_UPDATE };
        enum Criteria { Item, PR, PO };

        TabPage[] page = new TabPage[4];                        //Tab pages buffer
        List<Mode> modeStack = new List<Mode>();                //GUI level stack
        Criteria dataMode = Criteria.Item;                      //Search flag buffer
        
        DataCache items = new DataCache("ITEMS");               //Search Result buffer

        int top = -1, edit = -1;                                //Stack and record identifiers
        object[] updateddata = new object[2];                   //fID and fStamp buffers
        string prevSrc = "";                                    //Previous search pattern
    }
    //Item management
    public partial class frmInventorySystem
    {
        private void ItemLoadChoices()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            CodeScript s = new CodeScript("LOAD");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_QUERY, "SELECT fData FROM tbllookup WHERE fKey='ARTICLE' ORDER BY fData ASC");
            s.Add(CodeIns.DBU_QUERY, "SELECT fData FROM tbllookup WHERE fKey='UNIT' ORDER BY fData ASC");
            s.Add(CodeIns.DBU_QUERY, "SELECT fData FROM tbllookup WHERE fKey='INVENTORY_TYPE' ORDER BY fData ASC");
            if (modeStack[top - 1] == Mode.ITEM_UPDATE)
            {
                items.SetActivePage(1);
                s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fID, fStamp, fStockNum, fDescription, fType, fArticle, fUnit, fEstimatedCost, fStatus " +
                "FROM tbl_ip_itemrecord WHERE (fID='{0}' AND fStamp='{1}')", items.GetValue(edit, "fID"), Helper.ToMySQLDateTime(items.GetValue(edit, "fStamp"))));
            }
            s.Add(CodeIns.DBU_END);

            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Initialize Choices", "Cannot initialize choices due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        btnClose_Click(null, null);
                        return;
                    }
                    DataCache d;
                    c.DataFetch(out d);


                    d.SetActivePage(0);
                    txtItemArticle.Items.Clear();
                    txtItemArticle.Items.AddRange(d.GetObjectList(0));

                    d.SetActivePage(1);
                    txtItemUnit.Items.Clear();
                    txtItemUnit.Items.AddRange(d.GetObjectList(0));

                    d.SetActivePage(2);
                    txtItemType.Items.Clear();
                    txtItemType.Items.AddRange(d.GetObjectList(0));


                    if (modeStack[top - 1] == Mode.ITEM_UPDATE)
                    {
                        if (d.GetRecordCount(3) == 0)
                        {
                            Confirm.Error("Update", "Cannot fetch item record.");
                            PopMode();
                            PopMode();
                            ApplyMode();
                            return;
                        }

                        d.SetActivePage(3);

                        updateddata[0] = d.GetValue(0, "fID");
                        updateddata[1] = d.GetValue(0, "fStamp");
                        txtItemStockNumber.Text = d.GetValue(0, "fStockNum").ToString();
                        txtItemDescription.Text = d.GetValue(0, "fDescription").ToString();
                        txtItemType.Text = d.GetValue(0, "fType").ToString();
                        txtItemArticle.Text = d.GetValue(0, "fArticle").ToString();
                        txtItemUnit.Text = d.GetValue(0, "fUnit").ToString();
                        txtItemCost.Value = decimal.Parse(d.GetValue(0, "fEstimatedCost").ToString());
                        if (d.GetValue(0, "fStatus").ToString().Equals("PHASEOUT"))
                        {
                            chkItemStatus.Checked = true;
                        }
                        else
                        {
                            chkItemStatus.Checked = false;
                        }

                    }
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void ItemInsertRecord()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            string fstatus = "ACTIVE";
            if (chkItemStatus.Checked) fstatus = "PHASEOUT";

            CodeScript s = new CodeScript("INSERT");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_INSERT, "tbl_ip_itemrecord",
                //Fields
                new string[]{ "fStockNum", "fType", "fDescription",
                    "fArticle", "fUnit", "fEstimatedCost", "fStatus"},
                //Data
                new object[]{Sym.PQS(txtItemStockNumber.Text), Sym.PQS(txtItemType.Text),
                    Sym.PQS(txtItemDescription.Text), Sym.PQS(txtItemArticle.Text),
                    Sym.PQS(txtItemUnit.Text), Sym.PQS(txtItemCost.Value), Sym.PQS(fstatus)}
            );
            s.Add(CodeIns.DBU_END);
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Item Registration", "Cannot register item due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    Confirm.Info("Item Registration", "Registration successful.");
                    Clear(pnlItemForm.Controls);
                    PopMode();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void ItemUpdateRecord()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            string fstatus = "ACTIVE";
            if (chkItemStatus.Checked) fstatus = "PHASEOUT";
            CodeScript s = new CodeScript("UPDATE");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_UPDATE, "tbl_ip_itemrecord",
                //Fields
                new string[]{ "fStockNum", "fType", "fDescription",
                    "fArticle", "fUnit", "fEstimatedCost", "fStatus"},
                //Data
                new object[]{Sym.PQS(txtItemStockNumber.Text), Sym.PQS(txtItemType.Text),
                    Sym.PQS(txtItemDescription.Text), Sym.PQS(txtItemArticle.Text),
                    Sym.PQS(txtItemUnit.Text), Sym.PQS(txtItemCost.Value), Sym.PQS(fstatus)},
                //Where clause
                string.Format("fID='{0}' AND fStamp='{1}'", updateddata[0], Helper.ToMySQLDateTime(updateddata[1]))
            );
            s.Add(CodeIns.DBU_END);
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Item Registration", "Cannot register item due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    Confirm.Info("Item Registration", "Registration successful.");
                    Clear(pnlItemForm.Controls);
                    PopMode();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void ItemSearch()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            CodeScript s = new CodeScript("SEARCH");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fStockNum AS 'Stock No.', fDescription AS 'Description', " +
                "fType AS 'Type', fArticle AS 'Article', fUnit AS 'Unit' , fEstimatedCost AS 'Estimated Cost', fStatus AS 'Status' " +
                "FROM tbl_ip_itemrecord WHERE  " +
                "fStockNum LIKE '%{0}%' OR " +
                "fDescription LIKE '%{0}%' OR " +
                "fType LIKE '%{0}%' OR " +
                "fArticle LIKE '%{0}%' OR " +
                "fUnit LIKE '%{0}%' OR " +
                "fEstimatedCost LIKE '%{0}%' OR " +
                "fStatus LIKE '%{0}%' " +
                "ORDER BY fType ASC, fUnit ASC, fDescription ASC " +
                "LIMIT {1}, 50",
                prevSrc, (txtPage.Value - 1m) * 50)
                );
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fID, fStamp, fStockNum, fDescription, fType, fArticle, fUnit, fEstimatedCost " +
                "FROM tbl_ip_itemrecord WHERE  " +
                "fStockNum LIKE '%{0}%' OR " +
                "fDescription LIKE '%{0}%' OR " +
                "fType LIKE '%{0}%' OR " +
                "fArticle LIKE '%{0}%' OR " +
                "fUnit LIKE '%{0}%' OR " +
                "fEstimatedCost LIKE '%{0}%' OR " +
                "fStatus LIKE '%{0}%' " +
                "ORDER BY fType ASC, fUnit ASC, fDescription ASC " +
                "LIMIT {1}, 50",
                prevSrc, (txtPage.Value - 1m) * 50)
                );
            s.Add(CodeIns.DBU_END);
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Search", "Cannot perform search due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        btnClose_Click(null, null);
                        return;
                    }
                    c.DataFetch(out items);

                    if (items.GetRecordCount(0) == 50)
                        txtPage.Maximum = txtPage.Value + 1;
                    else
                        txtPage.Maximum = txtPage.Value;
                    
                    items.SetActivePage(0);
                    grdSearch.DataSource = items.ExportPage();
                    ItemFormatGrid();
                    Search_FormatGrid();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void ItemFormatGrid()
        {
            foreach (DataGridViewColumn dvc in grdSearch.Columns)
            {
                if (dvc.ValueType == typeof(float) || dvc.ValueType == typeof(double))
                {
                    dvc.DefaultCellStyle.Format = "#,#0.00";
                    dvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                }
            }
        }
    }
    //Purchase Request Management
    public partial class frmInventorySystem
    {
        DataCache prItems = new DataCache("PRITEMS");           //Purchase Request added items
        DataCache prData = new DataCache("PRDATA");             //Loaded Purchase Request Data

        private void PRInitItems()
        {
            prItems = new DataCache("PRITEMS");
            prItems.CreatePage("PRITEMS",
                new string[] { "Stock Number", "Description", "Quantity", "Unit", "EstimatedCost" },
                new Type[] { typeof(string), typeof(string), typeof(float), typeof(string), typeof(float) });
            prItems.SetActivePage(0);
            DataTable dt = prItems.ExportPage();
            dt.Columns.Add("Subtotal", typeof(float), "Quantity * EstimatedCost");
            grdPRItems.DataSource = dt;
            PRItemsFormatGrid();
        }
        private void PRItemsFormatGrid()
        {
            foreach (DataGridViewColumn dvc in grdPRItems.Columns)
            {
                if (dvc.ValueType == typeof(float))
                {
                    dvc.DefaultCellStyle.Format = "#,#0.00";
                    dvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                }
            }
        }
        private void PRAddItem(string stockNum, string desc, float qty, string unit, float cost) 
        {
            prItems.Insert(new object[] { stockNum, desc, qty, unit, cost });
        }
        private void PRRemoveAt(int index)
        {
            prItems.Delete(index);
        }
        private void PRAdjustQuantity(int index, float qty)
        {
            prItems.Edit(index, "Quantity", qty);
        }
        private void PRInsertRecord()
        {
            string fstatus = "PENDING";
            if (prItems.GetRecordCount(0) == 0)
            {
                Confirm.Generic("Create Purchase Request", "Please add atleast 1(one) item.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            PushMode(Mode.ENGINE);
            ApplyMode();
            
            CodeScript s = new CodeScript("INSERT");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            //Insert PR Details
            s.Add(CodeIns.DBU_INSERT, "tbl_ip_purchaserequest",
                //Fields
                new string[]{ "fPRNum", "fProjectName", "fProjectLocation", "fContractNum", "fDepartment", "fSection",
                    "fSAINum", "fPRDate", "fSAIDate", "fRequestor", "fDesignation", "fMayor","fStatus"},
                //Data
                new object[]{Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(txtPRProjectName.Text.Trim()), Sym.PQS(txtPRProjectLocation.Text.Trim()),
                     Sym.PQS(txtPRContractNum.Text.Trim()), Sym.PQS(txtPRDepartment.Text.Trim()), Sym.PQS(txtPRSection.Text.Trim()),
                    Sym.PQS(txtPRSAINum.Text.Trim()), Sym.PQS(txtPRSAIDate.Value.ToString("yyyy-MM-dd")), Sym.PQS(txtPRDate.Value.ToString("yyyy-MM-dd")),
                    Sym.PQS(txtPRRequestor.Text.Trim()), Sym.PQS(txtPRDesignation.Text.Trim()), 
                    Sym.PQS(txtPRMayor.Text.Trim()),Sym.PQS(fstatus)}
            );
            //Insert AB Details
            s.Add(CodeIns.DBU_INSERT, "tbl_ip_abstractofbids",
                new string[] { "fPRNum" },
                new object[] { Sym.PQS(txtPRNumber.Text.Trim())}
                );
            //Insert PR Items Here
            for (int i = 0; i < prItems.GetRecordCount(0); i++)
            {
                s.Add(CodeIns.DBU_INSERT, "tbl_ip_purchaserequestitems",
                    new string[] { "fPRNum", "fStockNum", "fDescription", "fQty", "fUnit", "fEstimatedCost" },
                    new object[]{Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(prItems.GetValue(i, "Stock Number")),
                        Sym.PQS(prItems.GetValue(i, "Description")), Sym.PQS(prItems.GetValue(i, "Quantity")),
                        Sym.PQS(prItems.GetValue(i, "Unit")), Sym.PQS(prItems.GetValue(i, "EstimatedCost"))
                    });
            }

            s.Add(CodeIns.DBU_END);
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Purchase Request", "Cannot save item due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    Confirm.Info("Purchase Request", "Save successful.");

                    Clear(pnlPRForm.Controls);
                    PopMode();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void PRUpdateRecord()
        {            if (prItems.GetRecordCount(0) == 0)
            {
                Confirm.Generic("Create Purchase Request", "Please add atleast 1(one) item.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            PushMode(Mode.ENGINE);
            ApplyMode();
            prData.SetActivePage(0);
            CodeScript s = new CodeScript("UPDATE");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            //Update the main PR
            s.Add(CodeIns.DBU_UPDATE, "tbl_ip_purchaserequest",
                new string[]{"fPRNum", "fProjectName", "fProjectLocation", "fContractNum", "fDepartment",
                "fSection", "fSAINum", "fSAIDate", "fPRDate", "fRequestor", "fDesignation", "fMayor"},
                new object[]{Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(txtPRProjectName.Text.Trim()), 
                    Sym.PQS(txtPRProjectLocation.Text.Trim()), Sym.PQS(txtPRContractNum.Text.Trim()),
                    Sym.PQS(txtPRDepartment.Text.Trim()), Sym.PQS(txtPRSection.Text.Trim()), 
                    Sym.PQS(txtPRSAINum.Text.Trim()), Sym.PQS(txtPRSAIDate.Value.ToString("yyyy-MM-dd")),
                    Sym.PQS(txtPRDate.Value.ToString("yyyy-MM-dd")),Sym.PQS(txtPRRequestor.Text.Trim()),
                    Sym.PQS(txtPRDesignation.Text.Trim()), Sym.PQS(txtPRMayor.Text.Trim())},
                string.Format("(fID='{0}') AND (fStamp='{1}')", prData.GetValue(0, "fID"), Helper.ToMySQLDateTime(prData.GetValue(0, "fStamp")))
                );
            //Update Abstract of Bids
            /*
            //Single update, multiple tables
            s.Add(CodeIns.DBU_UPDATE_ALLOWZERO, "tbl_ip_abstractofbids as t1, tbl_ip_abstractofbidssupplier as t2, tbl_ip_abstractofbidsitem as t3",
               new string[] { "t1.fPRNum", "t2.fPRNum", "t3.fPRNum" },
               new object[] { Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(txtPRNumber.Text.Trim()) },
               string.Format("t1.fPRNum='{0}' AND t2.fPRNum='{0}' AND t3.fPRNum='{0}'", prData.GetValue(0, "fPRNum"))
               );
            */
            s.Add(CodeIns.DBU_UPDATE, "tbl_ip_abstractofbids",
               new string[] { "fPRNum" },
               new object[] { Sym.PQS(txtPRNumber.Text.Trim()) },
               string.Format("fPRNum='{0}'", prData.GetValue(0, "fPRNum"))
               );
            s.Add(CodeIns.DBU_UPDATE_ALLOWZERO, "tbl_ip_abstractofbidssupplier",
               new string[] { "fPRNum" },
               new object[] { Sym.PQS(txtPRNumber.Text.Trim()) },
               string.Format("fPRNum='{0}'", prData.GetValue(0, "fPRNum"))
               );
            s.Add(CodeIns.DBU_UPDATE_ALLOWZERO, "tbl_ip_abstractofbidsitem",
               new string[] { "fPRNum" },
               new object[] { Sym.PQS(txtPRNumber.Text.Trim()) },
               string.Format("fPRNum='{0}'", prData.GetValue(0, "fPRNum"))
               );
            
            //Update PR Details
            prItems.SetActivePage(0);
            prData.SetActivePage(1);
            if (prData.GetRecordCount(1) == prItems.GetRecordCount(0))
            {
                for (int i = 0; i < prItems.GetRecordCount(0); i++)
                {
                    s.Add(CodeIns.DBU_UPDATE, "tbl_ip_purchaserequestitems",
                        new string[] { "fPRNum", "fStockNum", "fDescription", "fQty", "fUnit", "fEstimatedCost" },
                        new object[] {Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(prItems.GetValue(i, "Stock Number")),
                            Sym.PQS(prItems.GetValue(i, "Description")), Sym.PQS(prItems.GetValue(i, "Quantity")),
                            Sym.PQS(prItems.GetValue(i, "Unit")), Sym.PQS(prItems.GetValue(i, "EstimatedCost"))},
                        string.Format("(fID='{0}') AND (fStamp='{1}')", prData.GetValue(i, "fID"), Helper.ToMySQLDateTime(prData.GetValue(i, "fStamp")))
                        );
                }
            }
            else if (prData.GetRecordCount(1) < prItems.GetRecordCount(0))
            {
                for (int i = 0; i < prData.GetRecordCount(1); i++)
                {
                    s.Add(CodeIns.DBU_UPDATE, "tbl_ip_purchaserequestitems",
                        new string[] { "fPRNum", "fStockNum", "fDescription", "fQty", "fUnit", "fEstimatedCost" },
                        new object[] {Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(prItems.GetValue(i, "Stock Number")),
                            Sym.PQS(prItems.GetValue(i, "Description")), Sym.PQS(prItems.GetValue(i, "Quantity")),
                            Sym.PQS(prItems.GetValue(i, "Unit")), Sym.PQS(prItems.GetValue(i, "EstimatedCost"))},
                        string.Format("(fID='{0}') AND (fStamp='{1}')", prData.GetValue(i, "fID"), Helper.ToMySQLDateTime(prData.GetValue(i, "fStamp")))
                        );
                }
                for (int i = prData.GetRecordCount(1); i < prItems.GetRecordCount(0) ; i++)
                {
                    s.Add(CodeIns.DBU_INSERT, "tbl_ip_purchaserequestitems",
                    new string[] { "fPRNum", "fStockNum", "fDescription", "fQty", "fUnit", "fEstimatedCost" },
                    new object[]{Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(prItems.GetValue(i, "Stock Number")),
                        Sym.PQS(prItems.GetValue(i, "Description")), Sym.PQS(prItems.GetValue(i, "Quantity")),
                        Sym.PQS(prItems.GetValue(i, "Unit")), Sym.PQS(prItems.GetValue(i, "EstimatedCost"))
                    });
                }
            }
            else if (prData.GetRecordCount(1) > prItems.GetRecordCount(0))
            {
                for (int i = 0; i < prItems.GetRecordCount(0); i++)
                {
                    s.Add(CodeIns.DBU_UPDATE, "tbl_ip_purchaserequestitems",
                        new string[] { "fPRNum", "fStockNum", "fDescription", "fQty", "fUnit", "fEstimatedCost" },
                        new object[] {Sym.PQS(txtPRNumber.Text.Trim()), Sym.PQS(prItems.GetValue(i, "Stock Number")),
                            Sym.PQS(prItems.GetValue(i, "Description")), Sym.PQS(prItems.GetValue(i, "Quantity")),
                            Sym.PQS(prItems.GetValue(i, "Unit")), Sym.PQS(prItems.GetValue(i, "EstimatedCost"))},
                        string.Format("(fID='{0}') AND (fStamp='{1}')", prData.GetValue(i, "fID"), Helper.ToMySQLDateTime(prData.GetValue(i, "fStamp")))
                        );
                }
                for (int i = prItems.GetRecordCount(0); i < prData.GetRecordCount(1); i++)
                {
                    s.Add(CodeIns.DBU_DELETE, "tbl_ip_purchaserequestitems",
                        string.Format("(fID='{0}') AND (fStamp='{1}')", prData.GetValue(i, "fID"), Helper.ToMySQLDateTime(prData.GetValue(i, "fStamp")))
                        );
                }
            }
            s.Add(CodeIns.DBU_END);
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Purchase Request", "Cannot save item due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    Confirm.Info("Purchase Request", "Save successful.");

                    Clear(pnlPRForm.Controls);
                    PopMode();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void PRSearch()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            string datesearch =  Helper.ToMySQLDate(prevSrc).ToString();
            if (datesearch.Length == 0) datesearch = prevSrc;
            CodeScript s = new CodeScript("SEARCH");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT t1.fPRNum AS 'PR No.', fProjectName AS 'Project Name', " +
                "fRequestor AS 'Requestor', fDepartment AS 'Department', fSection AS 'Section' , fPRDate AS 'P.R. Date', fAmount AS 'Amount', fStatus AS 'Status' " +
                "FROM tbl_ip_purchaserequest t1, " +
                "(SELECT SUM(fQty * fEstimatedCost) AS fAmount, t22.fPRNum FROM tbl_ip_purchaserequestitems t22 GROUP BY t22.fPRNum) AS t2 WHERE  " +
                
                "(t1.fPRNum LIKE '%{0}%' OR " +
                "fProjectName LIKE '%{0}%' OR " +
                "fRequestor LIKE '%{0}%' OR " +
                "fDepartment LIKE '%{0}%' OR " +
                "fSection LIKE '%{0}%' OR " +
                "fPRDate LIKE '%{1}%' OR " +
                "fStatus LIKE '%{0}%') AND " +
                "(t1.fPRNum = t2.fPRNum) " +
                "ORDER BY fRequestor ASC, fPRDate DESC " +
                "LIMIT {2}, 50",
                prevSrc, datesearch, (txtPage.Value - 1m) * 50)
                );
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fID, fStamp, t1.fPRNum " +
                "FROM tbl_ip_purchaserequest t1, " +
                "(SELECT SUM(fQty * fEstimatedCost), t22.fPRNum FROM tbl_ip_purchaserequestitems t22 GROUP BY t22.fPRNum) AS t2 WHERE  " +

                "(t1.fPRNum LIKE '%{0}%' OR " +
                "fProjectName LIKE '%{0}%' OR " +
                "fRequestor LIKE '%{0}%' OR " +
                "fDepartment LIKE '%{0}%' OR " +
                "fSection LIKE '%{0}%' OR " +
                "fPRDate LIKE '%{1}%' OR " +
                "fStatus LIKE '%{0}%') AND " +
                "(t1.fPRNum = t2.fPRNum) " +
                "ORDER BY fRequestor ASC, fPRDate DESC " +
                "LIMIT {2}, 50",
                prevSrc, datesearch, (txtPage.Value - 1m) * 50)
                );
            s.Add(CodeIns.DBU_END);
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Search", "Cannot perform search due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        btnClose_Click(null, null);
                        return;
                    }
                    c.DataFetch(out items);
                    if (items.GetRecordCount(0) == 50)
                        txtPage.Maximum = txtPage.Value + 1;
                    else
                        txtPage.Maximum = txtPage.Value;

                    items.SetActivePage(0);
                    grdSearch.DataSource = items.ExportPage();
                    ItemFormatGrid();
                    Search_FormatGrid();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void PRLoadData()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            items.SetActivePage(1);
            CodeScript s = new CodeScript("LOAD");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_QUERY, 
                string.Format("SELECT fID, fStamp, fPRNum, fProjectName, fProjectLocation, " +
                "fContractNum, fDepartment, fSection, fSAINum, fPRDate, fSAIDate, fRequestor, " +
                "fDesignation, fMayor, fStatus FROM tbl_ip_purchaserequest WHERE (fID='{0}') AND (fStamp='{1}')", 
                items.GetValue(edit, "fID"), Helper.ToMySQLDateTime(items.GetValue(edit, "fStamp"))));
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fID, fStamp, fPRNum, fStockNum, fDescription, " +
                "fQty, fUnit, fEstimatedCost FROM tbl_ip_purchaserequestitems WHERE fPRNum='{0}'",
                items.GetValue(edit, "fPRNum")));
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fStockNum AS 'Stock Number', fDescription AS 'Description', " +
                "fQty AS 'Quantity', fUnit AS 'Unit', fEstimatedCost AS 'EstimatedCost' FROM tbl_ip_purchaserequestitems WHERE fPRNum='{0}'",
                items.GetValue(edit, "fPRNum")));
            s.Add(CodeIns.DBU_END);

            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                          c.State == ProcessorFlag.Error ||
                          c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Load Purchase Request", "Cannot load purchase request due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    c.DataFetch(out prData);
                    if (prData.GetRecordCount(0) == 0 || prData.GetRecordCount(1) == 0)
                    {
                        Confirm.Error("Load Purchase Request", "Cannot load purchase request due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    //Load PR Details
                    prData.SetActivePage(0);
                    txtPRNumber.Text = prData.GetValue(0, "fPRNum").ToString();
                    txtPRProjectName.Text = prData.GetValue(0, "fProjectName").ToString();
                    txtPRProjectLocation.Text = prData.GetValue(0, "fProjectLocation").ToString();
                    txtPRContractNum.Text = prData.GetValue(0, "fContractNum").ToString();
                    txtPRDepartment.Text = prData.GetValue(0, "fDepartment").ToString();
                    txtPRSection.Text = prData.GetValue(0, "fSection").ToString();
                    txtPRSAINum.Text = prData.GetValue(0, "fSAINum").ToString();
                    txtPRSAIDate.Value = DateTime.Parse(prData.GetValue(0, "fSAIDate").ToString());
                    txtPRDate.Value = DateTime.Parse(prData.GetValue(0, "fPRDate").ToString());
                    txtPRRequestor.Text = prData.GetValue(0, "fRequestor").ToString();
                    txtPRDesignation.Text = prData.GetValue(0, "fDesignation").ToString();
                    txtPRMayor.Text = prData.GetValue(0, "fMayor").ToString();
                    //Load PR Items
                    prItems = new DataCache("PRITEMS");
                    prItems.ImportPage(prData, 2);
                    prItems.SetActivePage(0);
                    DataTable dt = prItems.ExportPage();
                    dt.Columns.Add("Subtotal", typeof(float), "Quantity * EstimatedCost");
                    grdPRItems.DataSource = dt;
                    PRItemsFormatGrid();
                    PRForm_GridTotal();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void PRUpdateStatus(string fstatus)
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            prData.SetActivePage(0);
            CodeScript s = new CodeScript("UPDATE");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
  
            s.Add(CodeIns.DBU_UPDATE, "tbl_ip_purchaserequest",
                new string[]{"fStatus"},
                new object[]{Sym.PQS(fstatus)},
                string.Format("(fID='{0}') AND (fStamp='{1}')", prData.GetValue(0, "fID"), Helper.ToMySQLDateTime(prData.GetValue(0, "fStamp")))
                );
           
            s.Add(CodeIns.DBU_END);
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Purchase Request", "Cannot save item due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    Confirm.Info("Purchase Request", "Save successful.");

                    Clear(pnlPRForm.Controls);
                    PopMode();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
        private void PRPush(string msgType, string purpose, string fuid,  string prNumber, string requestor)
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            Common.NOTIFY.Push(msgType,
                string.Format("{0} - P.R.#{1} by {2}", purpose, prNumber, requestor),
                new string[] {prNumber, fuid.ToString()});
            PopMode();
            ApplyMode();
        }
    }
    //Abstract of Bids
    public partial class frmInventorySystem
    {
        //Slow implementation
        /*
        private void ABInitItems()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            CodeScript s = new CodeScript("LOAD_SUPPLIER");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_QUERY,
                string.Format("SELECT fID, fStamp, fPRNum, fProjectName, fProjectLocation, " +
                "fContractNum, fDepartment, fSection, fSAINum, fPRDate, fSAIDate, fRequestor, " +
                "fDesignation, fMayor, fStatus FROM tbl_ip_purchaserequest WHERE (fID='{0}') AND (fStamp='{1}')",
                items.GetValue(edit, "fID"), Helper.ToMySQLDateTime(items.GetValue(edit, "fStamp"))));
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fID, fStamp, fPRNum, fStockNum, fDescription, " +
                "fQty, fUnit, fEstimatedCost FROM tbl_ip_purchaserequestitems WHERE fPRNum='{0}'",
                items.GetValue(edit, "fPRNum")));
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT DISTINCT fSupplier FROM tbl_ip_abstractofbidssupplier WHERE fPRNum='{0}'", txtABPRNum.Text));
            s.Add(CodeIns.DBU_END);

            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Load Supplier", "Cannot load supplier due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        c.ClearErrorFlag();
                        return;
                    }
                    c.DataFetch(out abSupplier);
                    abSupplier.SetActivePage(0);
                    txtABSupplier.Items.Clear();
                    txtABSupplier.Items.AddRange(abSupplier.GetObjectList("fSupplier"));

                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
         * */
        //New implementation
        //Page 0 - Abstract of bids details
        //Page 1 - Supplier List
        //Page 2 - Purchase Request Item list
        DataCache abItems = new DataCache("ABITEMS");           //Abstaract of Bids supplier items
        DataCache abData = new DataCache("ABDATA");             //Loaded Abstract of Bids data.
        float curPRAmount = 0f;                                 //Buffer current PR amount to prevent requery.
        private void ABLoadData()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            CodeScript s = new CodeScript("LOAD_SUPPLIER");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            items.SetActivePage(1);
            s.Add(CodeIns.DBU_QUERY,
                string.Format("SELECT fID, fStamp, fPRNum, fProject, fBudget, fOpened, fTime, fVenue FROM " +
                "tbl_ip_abstractofbids t1, (SELECT fProjectName AS fProject FROM tbl_ip_purchaserequest as pr WHERE pr.fPRNum='{0}') as t2 " +
                "WHERE fPRNum='{0}'", items.GetValue(edit, "fPRNum"))
                );
            s.Add(CodeIns.DBU_QUERY,
                string.Format("SELECT fSupplier FROM tbl_ip_abstractofbidssupplier WHERE fPRNum='{0}'", items.GetValue(edit, "fPRNum"))
                );
            s.Add(CodeIns.DBU_QUERY,
                string.Format("SELECT fDescription, fQty, fUnit, fEstimatedCost FROM tbl_ip_purchaserequestitems WHERE fPRNum='{0}'", items.GetValue(edit, "fPRNum"))
                );
            s.Add(CodeIns.DBU_END);
            items.SetActivePage(0);
            curPRAmount = float.Parse(items.GetValue(edit, "Amount").ToString());
            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Load Supplier", "Cannot load supplier due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        c.ClearErrorFlag();
                        return;
                    }
                    c.DataFetch(out abData);
                    abData.SetActivePage(0);
                    txtABPRNum.Text = abData.GetValue(0, "fPRNum").ToString();
                    txtABProjectName.Text = abData.GetValue(0, "fProject").ToString();
                    txtABVenue.Text = abData.GetValue(0, "fVenue").ToString();
                    txtABPRAmount.Text = curPRAmount.ToString("#,#0.00");
                    //Load initial value
                    txtABBudget.Value = 0m;
                    txtABOpenedDate.Value = DateTime.Now;
                    txtABTime.Value = DateTime.Parse(string.Format("{0}:00:00", DateTime.Now.Hour));
                    try
                    {
                        txtABBudget.Value = decimal.Parse(abData.GetValue(0, "fBudget").ToString());
                        txtABOpenedDate.Value = DateTime.Parse(abData.GetValue(0, "fOpened").ToString());
                        txtABTime.Value = DateTime.Parse(abData.GetValue(0, "fTime").ToString());
                    }
                    catch { }
                    abData.SetActivePage(1);
                    txtABSupplier.Items.Clear();
                    txtABSupplier.Items.AddRange(abData.GetObjectList("fSupplier"));
                    if (txtABSupplier.Items.Count > 0)
                    {
                        txtABSupplier.SelectedIndex = 0;
                    }
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }//ABLoadData
        private void ABSaveData()
        {
            abData.SetActivePage(1);
           
            PushMode(Mode.ENGINE);
            ApplyMode();

            CodeScript s = new CodeScript("INSERT_SUPPLIER");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            abData.SetActivePage(0);
            s.Add(CodeIns.DBU_UPDATE, "tbl_ip_abstractofbids",
                new string[] { "fBudget", "fOpened", "fTime", "fVenue" },
                new object[] { Sym.PQS(txtABBudget.Value), Sym.PQS(txtABOpenedDate.Value.ToString("yyyy-MM-dd")), Sym.PQS(txtABTime.Value.ToString("HH:mm:ss")), Sym.PQS(txtABVenue.Text) },
                string.Format("(fID='{0}') AND (fStamp='{1}')", abData.GetValue(0, "fID"), Helper.ToMySQLDateTime(abData.GetValue(0, "fStamp")))
                );
            abData.SetActivePage(1);
            string[] sup = abData.GetStringList("fSupplier");
            //If the abstract of bids already contains the supplier,update it instead.
            if (sup.Contains(txtABSupplier.Text))
            {
                for (int i = 0; i < abItems.GetRecordCount(0); i++)
                {
                    abItems.SetActivePage(1);
                    object id = abItems.GetValue(i, "fID"), stamp = Helper.ToMySQLDateTime(abItems.GetValue(i, "fStamp"));
                    abItems.SetActivePage(0);
                    s.Add(CodeIns.DBU_UPDATE, "tbl_ip_abstractofbidsitem",
                        new string[] { "fAmount" },
                        new object[] { abItems.GetValue(i, "Price") },
                        string.Format("(fID='{0}') AND fStamp='{1}'", id, stamp)
                        );
                }
            }
            else
            {
                //If the supplier is not in the supplier list, add it.
                s.Add(CodeIns.DBU_INSERT, "tbl_ip_abstractofbidssupplier",
                    new string[] { "fPRNum", "fSupplier" },
                    new object[] { Sym.PQS(txtABPRNum.Text), Sym.PQS(txtABSupplier.Text) }
                    );

                abData.SetActivePage(2);
                for (int i = 0; i < abData.GetRecordCount(2); i++)
                {
                    s.Add(CodeIns.DBU_INSERT, "tbl_ip_abstractofbidsitem",
                        new string[] { "fPRNum", "fSupplier", "fItem", "fAmount" },
                        new object[] { Sym.PQS(txtABPRNum.Text), Sym.PQS(txtABSupplier.Text), Sym.PQS(abData.GetValue(i, "fDescription")), Sym.PQS(0f) }
                        );
                }
            }
            s.Add(CodeIns.DBU_END);

            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Save Supplier Details", "Cannot add supplier due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    Confirm.Info("Save Supplier Details", "Save successful.");
                    ABLoadData();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }//ABAddSupplier
        private void ABLoadSupplierBid()
        {
            PushMode(Mode.ENGINE);
            ApplyMode();
            CodeScript s = new CodeScript("LOAD_SUPPLIER_BID");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                   Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fItem AS 'Description', fAmount AS 'Price' FROM tbl_ip_abstractofbidsitem WHERE " +
                "fPRNum='{0}' AND fSupplier='{1}'", txtABPRNum.Text, txtABSupplier.Text));
            s.Add(CodeIns.DBU_QUERY, string.Format("SELECT fID, fStamp FROM tbl_ip_abstractofbidsitem WHERE " +
                "fPRNum='{0}' AND fSupplier='{1}'", txtABPRNum.Text, txtABSupplier.Text));
            s.Add(CodeIns.DBU_END);

            CodeProcessor c = new CodeProcessor();
            c.ProcessCompleted += new EventHandler(delegate
            {
                c.SafeCall(this, delegate
                {
                    if (c.State == ProcessorFlag.DataRdyErr ||
                           c.State == ProcessorFlag.Error ||
                           c.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Load Supplier Bids", "Cannot load supplier due to the error :\r\n" + c.GetProcessorMessage);
                        PopMode();
                        ApplyMode();
                        c.ClearErrorFlag();
                        return;
                    }
                    c.DataFetch(out abItems);
                    if (abItems.GetRecordCount(1) == 0)
                    {
                        Confirm.Error("Load Supplier Bids", "Supplier bids not found");
                        PopMode();
                        ApplyMode();
                        return;
                    }
                    abItems.SetActivePage(0);
                    grdABBids.DataSource = abItems.ExportPage();
                    AOBForm_FormatGrid();
                    PopMode();
                    ApplyMode();
                });
            });
            c.Run(s);
        }
    }//frmInventorySystem::SupplierModule
}